/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.KelasDAO;
import com.ivanbiz.dao.PengajarDAO;
import com.ivanbiz.dao.impl.KelasDAOImpl;
import com.ivanbiz.dao.impl.PengajarDAOImpl;
import com.ivanbiz.model.Pengajar;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author IW20149X
 */
public class PengajarActionService extends SelectorComposer<Component> {

    KelasDAO kelasDAO = new KelasDAOImpl();
    PengajarDAO pengajarDAO = new PengajarDAOImpl();
    SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
    @Wire
    Window win;
    @Wire
    Textbox npwp;
    @Wire
    Textbox nama;
    @Wire
    Textbox hp;
    @Wire
    Textbox email;
    @Wire
    Textbox alamat;
    Pengajar pengajar;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        pengajar = (Pengajar) Executions.getCurrent().getArg().get("pengajar");
    }

    @Listen("onClick=#simpan;")
    public void simpan() {
        if (pengajar == null) {
            pengajar = new Pengajar();
        }
        pengajar.setNIP(npwp.getValue());
        pengajar.setNama(nama.getValue());
        pengajar.setHandphone(hp.getValue());
        pengajar.setEmail(email.getValue());
        pengajar.setAlamat(alamat.getValue());
        pengajar.setDate(new Date());
        pengajar.setTelp(hp.getValue());
        validatePengajar(pengajar);
    }

    private void validatePengajar(Pengajar pengajar) {
        if (pengajar.getNama().trim().isEmpty()) {
            Messagebox.show("Nama tidak boleh kosong");
        } else {
            try {
                pengajarDAO.saveOrUpdate(pengajar);
                win.detach();
            } catch (Exception ex) {
                Logger.getLogger(PengajarActionService.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
